<template>

  <el-page-header @back="router.go(-1)" title="返回">
    <template #content>
      <span class="text-large font-600 mr-3">{{engineerId?'编辑':'添加'}}工程师</span>
    </template>
  </el-page-header>

  <div class="content">
    <el-form ref="engineerFormRef"
             style="max-width: 600px"
             :model="engineerForm"
             :rules="engineerFormRule"
             :inline="false"
             label-width="100px">

      <el-form-item label="账号:" prop="loginName">
        <el-input v-model="engineerForm.loginName" placeholder="请输入账号" maxlength="20"/>
      </el-form-item>

      <el-form-item label="姓名:" prop="name">
        <el-input v-model="engineerForm.name" placeholder="请输入姓名" maxlength="12"/>
      </el-form-item>

      <el-form-item label="手机号:" prop="phone">
        <el-input v-model="engineerForm.phone" placeholder="请输入手机号" maxlength="11"/>
      </el-form-item>

      <el-form-item label="菜品图片:" prop="image">
        <el-upload
            class="avatar-uploader"
            action="http://localhost/file/upload"
            :headers="{token:token}"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="engineerForm.photo" :src="engineerForm.photo" style="width:178px;height:178px;" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
        </el-form-item>


      <el-form-item>
        <el-button @click="handleSave">保存</el-button>
        <el-button type="primary" @click="handleCancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import {reactive,ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import {saveEngineer,findEngineerById} from "@/api/engineer"
import { ElMessage } from 'element-plus'

const router = useRouter();
const route = useRoute();
  

let engineerFormRef = reactive(null);
let engineerForm = reactive({
  name: '',
  loginName: '',
  phone: ''
})

let engineerFormRule = reactive({
  name: [
    {
      required: true,
      trigger: 'blur'
    }
  ],
  loginName: [
    {
      required: true,
      trigger: 'blur'
    }
  ],
  phone: [{required: true, trigger: 'blur'}]
})
 
let engineerId = route.query.engineerId;


if(engineerId){
  findEngineerById(engineerId).then(resp=>{
    
    engineerForm.id = resp.data.id;
    engineerForm.name = resp.data.name;
    engineerForm.loginName = resp.data.loginName;
    engineerForm.phone = resp.data.phone;

    engineerForm.photo = resp.data.photo; 
  })

}

//图片上传相关方法
const handleAvatarSuccess=(resp, file)=>{
  engineerForm.photo = resp.data;
}

const beforeAvatarUpload=(file)=>{
  const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isJPG) {
    ElMessage.error('上传头像图片只能是 JPG 格式!');
  }
  if (!isLt2M) {
    ElMessage.error('上传头像图片大小不能超过 2MB!');
  }
  return isJPG && isLt2M;
}



let handleSave = () => {
  //判断是否校验通过
  engineerFormRef.validate((valid) => {
    if (valid) {
      saveEngineer(engineerId,engineerForm).then((resp) => {
        //提示并且返回列表
        router.push({path: '/engineer/list'});
      });
    }
  });
}

</script>

<style scoped>
.content {
  margin-top: 40px;
}
</style>
